#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>

#include "virtual_serial_driver_ioctl.h"

int main(int argc, char *argv[])
{
    int fd;
    int ret;
    unsigned int baud;
    struct option opt = {8, 1, 1};

    fd = open("/dev/vserdev0", O_RDWR);
    if (-1 == fd)
        goto fail;

    baud = 9600;
    ret = ioctl(fd, VS_SET_BARD, baud);
    if (-1 == ret)
        goto fail;

    ret = ioctl(fd, VS_GET_BARD, baud);
    if (-1 == ret)
        goto fail;

    ret = ioctl(fd, VS_SET_FFMT, &opt);
    if (-1 == ret)
        goto fail;

    ret = ioctl(fd, VS_GET_FFMT, &opt);
    if (-1 == ret)
        goto fail;

    printf("baud rate: %d\n", baud);
    printf("frame format: %d%c%d\n", opt.datab, opt.parity == 0 ? 'N' : opt.parity == 1 ? 'O'
                                                                                        : 'E',
           opt.stopb);

    close(fd);
    exit(EXIT_SUCCESS);
fail:
    perror("ioctl test");
    exit(EXIT_SUCCESS);
}